package org.ejs.bean;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.naming.NamingException;

import client.*;

import org.ejs.network.ServerAddress;

@Stateful(mappedName = "FileManager")
@LocalBean
public class FileManager implements FileManagerRemote {

	private ServerAddress client;
	private List<String> files;
	private FileManagerRemote next;

	@EJB
	ServerManagerRemote master;

	public FileManager() {
	}

	@PostConstruct
	public void postConstruct() {
		files = new ArrayList<String>();
	}

	@Override
	public void connect(ServerAddress from) {
		this.client = from;
		//TODO : Appeler le master
		javax.naming.Context ic = null;
		try {
			ic = new javax.naming.InitialContext(ClientTools.giveMeJndiProperties());
		} catch (NamingException e) {
			e.printStackTrace();
		}
		ServerManagerRemote master = null;
		try {
			master = (ServerManagerRemote) ic.lookup("ejb:/ejs/ServerManager!org.ejs.bean.ServerManagerRemote");
		} catch (NamingException e) {
			e.printStackTrace();
		}
		System.out.println("good");
		System.out.println(master.getTest().size());
		//master.connect();
	}

	@Override
	public void disconnect() {
		//TODO : Appeler le master
		master.disconnect(this);
	}


	@Override
	public void add(String name) {
		files.add(name);
	}

	@Override
	public void delete(String name) {
		files.remove(name);
	}

	@Override
	public List<String> list() {
		return list(client);
	}

	@Override
	public List<String> list(ServerAddress addr) {
		if (addr.equals(client)) {
			return files;
		}
		List<String> remoteFiles = next.list(addr);
		remoteFiles.addAll(files);
		return remoteFiles;
	}

	public boolean copy(String file, ServerAddress from) {
		return copy(file, from, client);
	}

	@Override
	public boolean copy(String file, ServerAddress from, ServerAddress to) {
		if (to.equals(client)) {
			add(file);
			return true;
		}
		if (from.equals(client)) {
			return false;
		}
		return next.copy(file, from, to);
	}

	@Override
	public boolean move(String file, ServerAddress addr) {
		if (copy(file, addr)) {
			delete(file);
			return true;
		}
		return false;
	}

	@Override
	public void setNext(FileManagerRemote next) {
		this.next = next;
	}

	@Override
	public FileManagerRemote getNext() {
		return next;
	}

	@Override
	public ServerAddress getAddress() {
		return this.client;
	}

	@Override
	public void setAddress(ServerAddress client) {
		this.client = client;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((this.client == null) ? 0 : this.client.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof FileManager))
			return false;
		FileManager other = (FileManager) obj;
		if (this.client == null) {
			if (other.client != null)
				return false;
		} else if (!this.client.equals(other.client))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return client.toString();
	}

	public String getTest(){
		return "gezhyrdg";
	}

	public ServerAddress getClient() {
		return client;
	}

	public void setClient(ServerAddress client) {
		this.client = client;
	}
}
